package com.daocao.hua.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daocao.hua.R;
import com.daocao.hua.adapter.V3MyBorrowRecordAdapter;
import com.daocao.hua.api.Api;
import com.daocao.hua.constant.UserGlobal;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.ToastUtil;
import com.daocao.hua.util.Util;
import com.daocao.hua.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3MyBorrowRecordActivity extends BaseActivity implements V3MyBorrowRecordAdapter.OnChooseListener {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.nothing)
    TextView nothing;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private Unbinder unbinder;
    private V3MyBorrowRecordAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_borrow_record_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("我的借款");
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getMyBorrowList();
            }
        });
        adapter = new V3MyBorrowRecordAdapter(this);
        adapter.setOnChooseListener(this);
        list.setAdapter(adapter);
        getMyBorrowList();
    }

    public void getMyBorrowList() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        refresh.setRefreshing(true);

        Api.api_service.getDealListV3().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);

                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {
                    adapter.update(result.data);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onClickHetong(String id) {
        Intent intent = new Intent(this, LoanAgreementActivity.class);
        intent.putExtra("type", 2);
        intent.putExtra("deal_id", id);
        UserGlobal.info.deal_id = id;
        UserGlobal.info.type = 2;
        startActivity(intent);
    }

    @Override
    public void onClickWechat() {
        Util.startActivity(this, WechatChatActivity.class);
    }
}
